<template>
	<view class="content">
		<!-- #ifndef APP-NVUE || MP-TOUTIAO -->
		<view class="u-demo-block">
			<u-swiper :list="banner" previousMargin="30" nextMargin="30" circular :autoplay="true" radius="5"
				bgColor="#ffffff"></u-swiper>
		</view>
		<!-- #endif -->
		<view class="navlist">
			<navigator @tap="mapdpc">
				<view class="title">附近电站</view>
				<view class="desc">查看附近电站</view>
				<view class="navicon"><u-icon name="map" color="#23BF61" size="32"></u-icon></view>
			</navigator> 
			<navigator url="/pages/order/order">
				<view class="title">订单记录</view>
				<view class="desc">查看附近电站</view>
				<view class="navicon"><u-icon name="order" color="#23BF61" size="34"></u-icon></view>
			</navigator>
			<view class="clear"></view>
		</view>
		<view class="search" @click="gotolist">
			<u-search placeholder="搜索你想要去的电站" v-model="keyword" :showAction="false" height="35"></u-search>
		</view>
		<view class="fj">
			<view class="tit">
				<view class="txt">推荐电站</view>
				<view class="more" @tap="cxdw">重新定位>></view>
			</view>
			<view class="list">
				<view v-for="(item,index) in list" class="item" @click="gotodetail(item.id)">
					<view class="title">{{item.station_name}}</view>
					<view class="desc">{{item.station_adds}}</view>
					<view class="txt">
						<view class="num">
							<text class="num-su" v-if="item.portData.kx_num>0">闲</text>
							<text class="num-fa" v-if="item.portData.kx_num==0">满</text>
							<text class="num-facc" v-if="item.portData.kx_num==0">空闲{{item.portData.kx_num}}个插座</text>
							<text class="num-succ" v-if="item.portData.kx_num>0">空闲{{item.portData.kx_num}}个插座</text>
							<text class="num-allcc">共{{item.portData.num}}个插座</text>
						</view>
						<view class="detail">去充电</view>
						<view class="clear"></view>
					</view>
					<view class="gotomap"><u-icon name="map-fill" color="#23BF61" size="20"></u-icon> <text>{{item.dist}}</text> </view>
				</view>
			</view>
		</view>
		<com-map ref="commap" @myevent="setlocation"></com-map>
		<com-tabbar :currentTab="currentTab"></com-tabbar>
	</view>
</template>

<script>
	let that
	import {
		mapState,
		mapActions
	} from 'vuex'
	export default {
		data() {
			return {
				banner: [
					'https://cdn.uviewui.com/uview/swiper/swiper3.png',
					'https://cdn.uviewui.com/uview/swiper/swiper2.png',
					'https://cdn.uviewui.com/uview/swiper/swiper1.png',
				],
				currentTab: "home",
				keyword: '',
				list:[],
				latitude: '',
				longitude: '',
			}
		},
		onLoad() {
			
		},
		onShow() {
			// #ifndef MP-WEIXIN
			uni.hideTabBar()
			// #endif
			this.getlist()
		},
		mounted() {
			that = this
		},
		computed: {
			//引入store全局变量
			...mapState(['mylocation','mylocationState'])
		},
		methods: {
			getlist(){
				//判断定位状态
				if (!this.mylocationState) {
					this.$refs.commap.setlocation()
					return
				}else{
					this.latitude = this.mylocation.latitude
					this.longitude = this.mylocation.longitude
					console.log('已经获取定位:lat='+this.latitude + ' lng='+ this.longitude)
				}
				this.$http_api.station_list({
					type: 2,
					latitude:this.latitude,
					longitude:this.longitude
				}).then((res)=>{
					that.list = res
					console.log(JSON.stringify(that.list))
				})
			},
			//跳转详情
			gotodetail(id){
				uni.navigateTo({
					url:"/pages/detail/detail-electromobile?id="+id
				})
			},
			//重新定位
			cxdw(){
				this.getlist()
			},
			//跳转电站地图
			mapdpc(){
				uni.switchTab({
					url:'/pages/article/article'
				})
			},
			//跳转搜索列表
			gotolist(){
				uni.navigateTo({
					url:'/pages/list/list'
				})
			},
			//子组件传值
			setlocation(e){
				this.latitude = e.latitude
				this.longitude = e.longitude
				this.getlist()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		padding: 0 3%;
		box-sizing: border-box;
	}
	
	.u-demo-block{
		width: 100%;
		margin-top: 20rpx;
	}
	.navlist{
		width: 100%;
		margin-top: 40rpx;
	}
	.navlist navigator{
		width: 48%;
		margin-right: 4%;
		padding: 50rpx 30rpx;
		float: left;
		box-shadow:0px 0px 5px #ccc;
		box-sizing: border-box;
		position: relative;
	}
	.navlist navigator:nth-child(2n){
		margin-right: 0;
	}
	.navlist navigator .title{
		font-size: 36rpx;
		font-weight: bold;
	}
	.navlist navigator .desc{
		font-size: 26rpx;
		color: #777;
		margin-top: 10rpx;
	}
	.navicon{
		position: absolute;
		right: 20rpx;
		top: 0;
		bottom: 0;
		margin: auto 0;
		height: 68rpx;
	}
	.search{
		margin-top: 40rpx;
	}
	.fj{
		margin-top: 60rpx;
		.tit{
			display: flex;
			justify-content: space-between;
			margin-bottom: 60rpx;
			.txt{
				font-size: 40rpx;
				font-weight: bold;
				padding-left: 20rpx;
				border-left: 3px solid #23BF61;
				line-height: 36rpx;
			}
			.more{
				font-size: 28rpx;
				color: #777;
			}
		}
		.list{
			.item{
				box-shadow:0px 0px 6px #dfdfdf;
				padding: 20rpx;
				margin-bottom: 40rpx;
				position: relative;
				.gotomap{
					position: absolute;
					right: 20rpx;
					top: 25rpx;
					display: flex;
					font-size: 28rpx;
					color: #333;
					text{
						margin-left: 3px;
					}
				}
				.title{
					font-size: 34rpx;
					font-weight: bold;
					width: 55%;
					overflow:hidden;
					white-space:nowrap;
					text-overflow:ellipsis;
				}
				.desc{
					font-size: 30rpx;
					color: #333;
					margin-top: 10rpx;
					margin-bottom: 20rpx;
					overflow:hidden;
					white-space:nowrap;
					text-overflow:ellipsis;
				}
				.txt{
					font-size: 26rpx;
					.num{
						float: left;
						margin-top: 6rpx;
						.num-su{
							padding: 4rpx 8rpx;
							background: #23BF61;
							color: #fff;
							border-radius: 6rpx;
							margin-right: 15rpx;
						}
						.num-fa{
							padding: 4rpx 8rpx;
							background: #f10000;
							color: #fff;
							border-radius: 6rpx;
							margin-right: 15rpx;
						}
						.num-succ{
							color: #23BF61;
						}
						.num-facc{
							color: #f10000;
						}
						.num-allcc{
							margin-left: 15rpx;
							padding-left: 15rpx;
							border-left: 1px solid #999;
						}
					}
					.detail{
						font-size: 28rpx;
						color: #23BF61;
						border: 1px solid #23BF61;
						border-radius: 40rpx;
						width: 130rpx;
						text-align: center;
						line-height: 46rpx;
						float: right;
					}
				}
			}
		}
		
	}
</style>